#!/usr/bin/env python3

# Copyright (c) 2020-2025 Fpemud <fpemud@sina.com>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.


import subprocess
from gi.repository import Gio
from gi.repository import GLib


class RunUtil:

    @staticmethod
    def systemdIsUnitRunning(unitName):
        obj = Gio.DBusProxy.new_for_bus_sync(Gio.BusType.SYSTEM,
                                             Gio.DBusProxyFlags.NONE,
                                             None,
                                             "org.freedesktop.systemd1",            # bus_name
                                             "/org/freedesktop/systemd1",           # object_path
                                             "org.freedesktop.systemd1.Manager")    # interface_name
        try:
            unit = obj.GetUnit("(s)", unitName)
        except GLib.GError as e:
            if e.matches(Gio.io_error_quark(), 36):
                # this exception would be raised if this unit is not enabled and not started
                # 36 -> org.freedesktop.systemd1.NoSuchUnit
                return False
            raise

        obj = Gio.DBusProxy.new_for_bus_sync(Gio.BusType.SYSTEM,
                                             Gio.DBusProxyFlags.NONE,
                                             None,
                                             "org.freedesktop.systemd1",                          # bus_name
                                             unit,                                                # object_path
                                             "org.freedesktop.DBus.Properties")                   # interface_name (don't use "org.freedesktop.systemd1.Unit")
        return obj.Get("(ss)", "org.freedesktop.systemd1.Unit", "ActiveState") == "active"        # don't use 'obj.ActiveState == "active"'

    @staticmethod
    def systemdStartUnit(unitName):
        subprocess.check_call(["systemctl", "start", unitName])

    @staticmethod
    def systemdStopUnit(unitName):
        subprocess.check_call(["systemctl", "stop", unitName])

    @staticmethod
    def systemdIsolateTarget(unitName):
        subprocess.check_call(["systemctl", "isolate", unitName])
